package xyz.xuminghai.javafx_learn.other;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * 2023/11/5 1:59 星期日<br/>
 * 线性映射
 *
 * @author xuMingHai
 */
public class LinearMapping extends Application {

    private final GridPane root = new GridPane(20.0, 50.0);

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() throws Exception {
        /*
          移动速度和音频速率进行线性映射
          normalizedSpeed = (currentSpeed - minSpeed) / (maxSpeed - minSpeed)
          normalizedRate = (currentRate - minRate) / (maxRate - minRate)
          normalizedSpeed = [0,1]  normalizedRate = [0,1]
          rate = normalizedSpeed * (maxRate - minRate) + minRate
         */
        Slider speedSlider = new Slider(1.0, 100.0, 50.0);
        Label speedLabel = new Label(String.valueOf(speedSlider.getValue()));
        speedLabel.setMaxWidth(50.0);
        Tooltip speedTooltip = new Tooltip();
        // 显示之前获取标签的文本
        speedTooltip.setOnShowing(event -> speedTooltip.setText(speedLabel.getText()));
        speedLabel.setTooltip(speedTooltip);

        Slider rateSlider = new Slider(0.125, 25.0, 10.0);
        Label rateLabel = new Label(String.valueOf(rateSlider.getValue()));
        rateLabel.setMaxWidth(50.0);
        Tooltip rateTooltip = new Tooltip();
        rateTooltip.setOnShowing(event -> rateTooltip.setText(rateLabel.getText()));
        rateLabel.setTooltip(rateTooltip);

        speedSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
            // normalizedSpeed = [0,1]
            double normalizedSpeed = (newValue.doubleValue() - speedSlider.getMin()) / (speedSlider.getMax() - speedSlider.getMin());
            // 计算出对应的rate值
            double rate = normalizedSpeed * (rateSlider.getMax() - rateSlider.getMin()) + rateSlider.getMin();
            rateSlider.setValue(rate);
            speedLabel.setText(newValue.toString());
        });

        rateSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
            double normalizedRate = (newValue.doubleValue() - rateSlider.getMin()) / (rateSlider.getMax() - rateSlider.getMin());
            double speed = normalizedRate * (speedSlider.getMax() - speedSlider.getMin()) + speedSlider.getMin();
            speedSlider.setValue(speed);
            rateLabel.setText(newValue.toString());
        });

        root.setAlignment(Pos.CENTER);
        root.addRow(0, new Label("速度"), speedSlider, speedLabel);
        root.addRow(1, new Label("速率"), rateSlider, rateLabel);

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
